package com.qfedu.stussm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.stussm.dao.TeacherDao;
import com.qfedu.stussm.dto.TeacherDto;
import com.qfedu.stussm.entity.Teacher;
import com.qfedu.stussm.service.intf.TeacherService;
import com.qfedu.stussm.vo.PageBeanVo;
import com.qfedu.stussm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherDao dao;
    @Override
    public R add(Teacher teacher) {
       if(dao.add(teacher) > 0){
           return R.ok();
       }else{
           return R.fail();
       }
    }

    @Override
    public PageBeanVo selectLikeAll(String t_name,int page, int limit) {
        PageHelper.startPage(page,limit);
        if(t_name != null && t_name.length() > 0){
            t_name="%"+t_name+"%";
        }else{
            t_name = null;
        }
        PageInfo<TeacherDto> teacherPageInfo = new PageInfo<>(dao.selectLikeAll(t_name));
        return new PageBeanVo(teacherPageInfo.getTotal(),teacherPageInfo.getList());
    }

    @Override
    public R delete(int t_id) {
        if(dao.delete(t_id) > 0){
            return R.ok();
        }else{
            return R.fail();
        }
    }

    @Override
    public R update(Teacher teacher) {
        if(dao.update(teacher) > 0){
            return R.ok();
        }else{
            return R.fail();
        }
    }

    @Override
    public R all() {
       return R.ok(dao.all());
    }
}
